package models;
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.Format;
import java.util.ArrayList;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import play.db.jpa.Model;


public class XMLoutput 
{
	private ArrayList<Tweet> tweets;
	
	public XMLoutput() throws IOException
    {
	}
	
	public void generator(ArrayList<Tweet> tweets2) throws IOException
	{
		this.tweets = tweets2;
    	
		Element root = new Element("tweets");
        Document dokument = new Document(root);
        
        for(int i = 0; i < tweets.size(); i++)
        {
        	if(tweets.get(i).getLanguage().equals("en"))
        	{
        		root.addContent(new Element("tweet")
        		.addContent(new Element("user").setText(tweets.get(i).getUser()))
        		.addContent(new Element("text").setText(tweets.get(i).getText()))
        		.addContent(new Element("updated").setText(tweets.get(i).getUpdated()))
        		.addContent(new Element("language").setText(tweets.get(i).getLanguage()))
        		.addContent(new Element("location").setText(tweets.get(i).getLocation()))
        		.addContent(new Element("picture").setText(tweets.get(i).getPicture()))
        		.addContent(new Element("textSent").setText(tweets.get(i).getTextSent()))
        		.addContent(new Element("rating").setText(tweets.get(i).getRatingString()))
        		);
        		
        		/*root.addContent(new Element("tweet")
	        		.setAttribute("user", tweets.get(i).getUser())
	        		.setAttribute("text", tweets.get(i).getText())
	        		.setAttribute("updated", tweets.get(i).getUpdated())
	        		.setAttribute("language", tweets.get(i).getLanguage())
	        		.setAttribute("location", tweets.get(i).getLocation())
	        		.setAttribute("picture", tweets.get(i).getPicture())
	        		.setAttribute("textSent", tweets.get(i).getTextSent())
	        		.setAttribute("rating", tweets.get(i).getRatingString())
	        		);*/
        	}
        }
    	
        XMLOutputter outputter = new XMLOutputter();
        java.io.FileWriter writer = new java.io.FileWriter("Tweetalyze/app/result.xml");
        outputter.output(dokument,writer);
	}
}